package com.shangxia.monomer.study.designPatterns.behavioralType.observer;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhouya38158
 * @Package com.shangxia.monomer.study.designPatterns.behavioralType.observer
 * @date 2021/8/6 14:11
 */
public class ObserverDemoSed {
    public static void main(String[] args) {

        Worker worker1 = new Worker("1");
        Worker worker2 = new Worker("2");
        Worker worker3 = new Worker("3");
        List<Worker> list = new ArrayList<>();
        list.add(worker1);
        list.add(worker2);
        list.add(worker3);
        for (Worker worker : list) {
            worker.rest();
        }
        WorkerObserver workerObserver = new WorkerObserver("前台");
        workerObserver.remind(list);
    }

}

interface ObserverPerson<T> {
    void remind(List<T> list);
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class WorkerObserver implements ObserverPerson<Worker> {
    private String name;

    @Override
    public void remind(List<Worker> list) {
        list.forEach(worker -> {
                    System.out.println(worker.getName());
                    worker.doJob();
                }
        );
    }
}

@Data
@AllArgsConstructor
class Worker {
    private String name;

    public void doJob() {
        System.out.println("work");
    }

    public void rest() {
        System.out.println(name + "rest");
    }

}

